package cn.szabadsag.org.service.impl;

import cn.szabadsag.base.service.impl.BaseServiceImpl;
import cn.szabadsag.org.domain.Employee;
import cn.szabadsag.org.domain.Shop;
import cn.szabadsag.org.domain.vo.ShopRejectVo;
import cn.szabadsag.org.mapper.EmployeeMapper;
import cn.szabadsag.org.mapper.ShopMapper;
import cn.szabadsag.org.service.IShopService;
import cn.szabadsag.user.domain.LoginInfo;
import cn.szabadsag.user.mapper.LoginInfoMapper;
import cn.szabadsag.utils.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.UUID;

/**
 * @author yangcong
 * @date 2022年03月04日 17:27
 * @since
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {


    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LoginInfoMapper loginInfoMapper;


    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        shopMapper.patchDel(ids);
    }

    /**
     * 商铺入驻信息校验
     * @param shop
     */
    @Override
    public void settledIn(Shop shop) {
        /*
        //  1.保存admin到t_employee
        employeeMapper.save(shop.getAdmin());
        //  2.保存shop到t_shop
        shopMapper.save(shop);*/
        //保存 t_employee  返回主键
       /* employeeMapper.save(shop.getAdmin());
        shopMapper.save(shop);
        //更新t_employee 里面 shop_id字段
        shop.getAdmin().setShop_id(shop.getId());
        employeeMapper.update(shop.getAdmin());*/

        Employee employee = shop.getAdmin();

        String salt = UUID.randomUUID().toString();
        //  MD5加密
        String password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(password);
        employee.setSalt(salt);
        /**
         * 1表示启用/激活
         */
        employee.setState(1);

        LoginInfo loginInfo = employee2LoginInfo(employee);

        loginInfoMapper.save(loginInfo);
        //  设置t_logininfo 里面 logininfo_id字段
        employee.setLogininfo_id(loginInfo.getId());
        employeeMapper.save(employee);
        shopMapper.save(shop);
        //  设置t_employee 里面 shop_id字段
        shop.getAdmin().setShop_id(shop.getId());
        employeeMapper.update(employee);

    }

    /**
     * 将Employee对象转化成LoginInfo对象
     * @param employee
     * @return
     */
    private LoginInfo employee2LoginInfo(Employee employee) {

        LoginInfo loginInfo = new LoginInfo();

        //  属性拷贝
        BeanUtils.copyProperties(employee,loginInfo);

        //  1表示前端用户，0表示后端管理员账户
        loginInfo.setType(0);
        //  激活状态
        loginInfo.setDisable(employee.getState());

        return loginInfo;
    }

    /**
     * 审核通过
     * @param id
     */
    @Override
    public void passShopReg(Long id) {

        //  根据id获取shop
        Shop shop = shopMapper.loadById(id);
        //  激活shop
        shop.setState(1);
        //  获取员工信息
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        //  激活employee
        //employee.setState(1);
        Long empId = employee.getId();

        shopMapper.update(shop);
        employeeMapper.update(employee);

        //  发送邮件，通知激活
        sendEmail(empId);

    }

    /**
     * 驳回商铺申请
     * @param shopRejectVo
     */
    @Override
    public void reject(ShopRejectVo shopRejectVo) {
        //  发送邮件，通知重新提交申请
        sendRejectEmail(shopRejectVo.getShop_Id(),shopRejectVo.getDesc());
    }


    /**
     * @param empId 被驳回用户的id
     */
    private void sendRejectEmail(Long empId,String desc){

        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();

            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
            //设置发件人
            helper.setFrom("cong.vip.2011@qq.com");
            //设置收件人
            helper.setTo("yccommon@163.com");
            //设置主题
            helper.setSubject("请重新更改商铺申请：" + desc);
            //设置内容
            helper.setText("<h>testContent</h>\n" +
                    "    <a href=\"http://localhost:8081/#/uptShopRegister?id=" + empId + "\">\n" +
                    "    <button>点击此处修改申请</button>\n" +
                    "    </a>",true);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param empId 待激活用户的id
     */
    private void sendEmail(Long empId){

        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();

            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
            //设置发件人
            helper.setFrom("cong.vip.2011@qq.com");
            //设置收件人
            helper.setTo("yccommon@163.com");
            //设置主题
            helper.setSubject("商铺账户激活：");
            //设置内容
            helper.setText("<h>testContent</h>\n" +
                    "    <a href=\"http://127.0.0.1:8080/employee/active?id=" + empId + "\">\n" +
                    "    <button>点击此处激活</button>\n" +
                    "    </a>",true);
            //设置附件、
            //helper.addAttachment("激活.rar",new File("C:\\Users\\yangcong\\Desktop\\新建文件夹\\七组作业.rar"));

            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
